using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using MyClever.Lib;

namespace MyCleverMissionEditor
{
    public partial class PluginSheet : UserControl
    {
        private MissionPluginProxy plugin;
        private int borderX;
        private int borderY;

        public PackageSheetAccordion PackageAccordion
        {
            get
            {
                return this.accordion;
            }
        }

        public int PluginID
        {
            get { return this.plugin.ID;  }
        }

        public PluginSheet(MissionPluginProxy plugin)
        {           
            InitializeComponent();

            this.plugin = plugin;
            this.borderX = 40;
            this.borderY = 40;

            this.accordion.Refreshed += new EventHandler(OnRefreshSize);

            this.lblPurpose.Text = this.plugin.ClassName;
            this.lblDescription.Text = this.plugin.ProgrammingLanguage.ToString();
        }

        public void Sheet_Load(object sender, EventArgs e)
        {
            foreach (MissionPluginPackage packageOn in this.plugin.GetPackages(Direction.All))
            {
                // create every package sheet
                PackageSheet packageSheetOn = new PackageSheet(packageOn);
                packageSheetOn.BorderStyle = BorderStyle.FixedSingle;
                this.accordion.Add(packageSheetOn);
            }
            this.RefreshSize();
        }

        private void OnRefreshSize(object sender, EventArgs e)
        {
            this.RefreshSize();
        }

        private void RefreshSize()
        {
            this.Size = new Size(this.accordion.Width + 40, this.accordion.Height + this.pnlHeadline.Height + 50);
        }

        private void grpBxPlugin_Paint(object sender, PaintEventArgs e)
        {
            // while this is much more faster, than draw the whole arrows on doubleBufferedTabControl, we draw some arrows here!!
            if (false == this.DesignMode)
            {
                foreach (PackageSheet packageSheetOn in this.accordion.PackageSheets)
                {
                    if (null != packageSheetOn.SlotCollection)
                    {
                        foreach (Point[] linesOn in packageSheetOn.SlotCollection.CalcLinesToDraw(this))
                        {
                            e.Graphics.DrawLines(new Pen(Brushes.Green, 3), linesOn);
                        }
                    }
	            }
            }
        }

        public bool IsMouseOver(Point mousePosition, bool withBorder)
        {
            if (true == withBorder)
            {
                return (mousePosition.Y > this.Location.Y - this.borderY && mousePosition.Y < this.Location.Y + this.Size.Height + this.borderY &&
                        mousePosition.X > this.Location.X - this.borderX && mousePosition.X < this.Location.X + this.Size.Width + this.borderX);
            }

            return (mousePosition.Y > this.Location.Y && mousePosition.Y < this.Location.Y + this.Size.Height &&
                        mousePosition.X > this.Location.X && mousePosition.X < this.Location.X + this.Size.Width);
        }

        public RelativePluginPosition GetPlaceholderByMouse(Point mousePosition, out Rectangle placeholder)
        {
            Debug.Assert(true == this.IsMouseOver(mousePosition, true));

            int posX = this.Location.X;
            int posY = this.Location.Y;

            DockOrientation dockOrientation = DockOrientation.None;
            if (mousePosition.Y > this.Location.Y + this.Height - this.borderY && mousePosition.Y < this.Location.Y + this.Height + this.borderY)
            {
                dockOrientation = DockOrientation.Down;
                posY += this.Size.Height + 10;
            }
            if (mousePosition.X > this.Location.X - this.borderX && mousePosition.X < this.Location.X + this.borderX)
            {
                dockOrientation = DockOrientation.Left;
                posX -= 10;
            }
            if (mousePosition.X > this.Location.X + this.Width - this.borderX && mousePosition.X < this.Location.X + this.Width + this.borderX)
            {
                dockOrientation = DockOrientation.Right;
                posX += this.Size.Width + 10;
            }

            placeholder = new Rectangle();
            if (DockOrientation.Left == dockOrientation || DockOrientation.Right == dockOrientation)
            {
                placeholder = new Rectangle(posX, this.Location.Y, 2, this.Size.Height);
            }
            if (DockOrientation.Down == dockOrientation)
            {
                placeholder = new Rectangle(this.Location.X, posY, this.Size.Width, 2);
            }

            return new RelativePluginPosition(dockOrientation, this.PluginID);
        }
    }
}
